﻿using System;
using System.Globalization;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.EntityModel;
using Resources;

namespace MonkeyWarranty.Web.Suppliers
{
    public partial class Edit : System.Web.UI.Page
    {
        #region Properties

        private SupplierBiz _supplierBiz;
        private SupplierBiz SupplierManager
        {
            get { return _supplierBiz ?? (_supplierBiz = new SupplierBiz()); }
        }

        private int _supplierId;
        private int SupplierId
        {
            get
            {
                if (_supplierId <= 0)
                    int.TryParse(Request.QueryString["Id"], out _supplierId);
                return _supplierId;
            }
        }

        private Supplier _supplier;
        private Supplier CurrentSupplier
        {
            get
            {
                if (_supplier == null && SupplierId > 0)
                {
                    _supplier = SupplierManager.GetById(SupplierId);
                }
                return _supplier;
            }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Load data
                // load supplier groups
                var groups = SupplierManager.GetAllSupplierGroups();//_repository.CityRepository.GetAllSupplierGroups();
                groups.Insert(0, new SupplierGroup { Id = 0 });
                ddlSupplierGroups.DataTextField = "Name";
                ddlSupplierGroups.DataValueField = "Id";
                ddlSupplierGroups.DataSource = groups;
                ddlSupplierGroups.DataBind();

                // Load supplier data
                if (CurrentSupplier == null)
                {
                    lblErrorMessage.Text = string.Format("{0}: Id={1}", GlobalStrings.NotExisted, SupplierId);
                    btnSave.Enabled = false;
                }
                else
                {
                    ddlSupplierGroups.SelectedValue = CurrentSupplier.SupplierGroupId.ToString();
                    txtCode.Text = CurrentSupplier.Code;
                    txtName.Text = CurrentSupplier.Name;
                    txtAddress.Text = CurrentSupplier.Address;
                    txtPhone.Text = CurrentSupplier.Phone;
                    txtFax.Text = CurrentSupplier.Fax;
                    txtEmail.Text = CurrentSupplier.Email;
                    txtWebsite.Text = CurrentSupplier.Website;
                    txtTaxCode.Text = CurrentSupplier.TaxCode;
                    txtBankAccount.Text = CurrentSupplier.BankAccount;
                    txtBankName.Text = CurrentSupplier.BankName;
                    txtBankProvince.Text = CurrentSupplier.BankProvince;
                    txtDirectorName.Text = CurrentSupplier.DirectorName;
                    txtAccounterName.Text = CurrentSupplier.AccounterName;
                    txtDescription.Text = CurrentSupplier.Description;
                    txtSortOrder.Text = CurrentSupplier.SortOrder.ToString(CultureInfo.InvariantCulture);
                    chkHidden.Checked = CurrentSupplier.Hidden;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;
            errorMessage.Visible = false;

            // validate inputs
            var code = txtCode.Text.Trim();
            if (code.Length == 0)
            {
                lblErrorMessage.Text = GlobalStrings.CodeIsRequired;
                errorMessage.Visible = true;
                return;
            }
            var name = txtName.Text.Trim();
            if (name.Length == 0)
            {
                lblErrorMessage.Text = GlobalStrings.NameIsRequired;
                errorMessage.Visible = true;
                return;
            }
            var descrition = txtDescription.Text;
            int sortOrder;
            int.TryParse(txtSortOrder.Text.Trim(), out sortOrder);
            var isHidden = chkHidden.Checked;
            var address = txtAddress.Text.Trim();
            var phone = txtPhone.Text.Trim();
            var fax = txtFax.Text.Trim();
            var email = txtEmail.Text.Trim();
            var website = txtWebsite.Text.Trim();
            var taxcode = txtTaxCode.Text.Trim();
            var bankAccount = txtBankAccount.Text.Trim();
            var bankName = txtBankName.Text.Trim();
            var bankProvince = txtBankProvince.Text.Trim();
            var director = txtDirectorName.Text.Trim();
            var accounter = txtAccounterName.Text.Trim();
            int supplierGroupId;
            int.TryParse(ddlSupplierGroups.SelectedValue, out supplierGroupId);

            // get data
            CurrentSupplier.Code = code;
            CurrentSupplier.Name = name;
            CurrentSupplier.Address = address;
            CurrentSupplier.Phone = phone;
            CurrentSupplier.Fax = fax;
            CurrentSupplier.Email = email;
            CurrentSupplier.Website = website;
            CurrentSupplier.TaxCode = taxcode;
            CurrentSupplier.BankAccount = bankAccount;
            CurrentSupplier.BankName = bankName;
            CurrentSupplier.BankProvince = bankProvince;
            CurrentSupplier.DirectorName = director;
            CurrentSupplier.AccounterName = accounter;
            CurrentSupplier.Description = descrition;
            CurrentSupplier.SortOrder = sortOrder;
            CurrentSupplier.Hidden = isHidden;
            CurrentSupplier.SupplierGroupId = supplierGroupId;

            // save data
            SupplierManager.UpdateSupplier(CurrentSupplier);

            lblErrorMessage.Text = GlobalStrings.UpdateSuccess;
            errorMessage.Visible = true;
        }

        #endregion
    }
}